<?php
session_start();
// Created by: or ANGGI la
// on: Wednesday, January, 21 2009
// version: 0.0.0.2
// :)

// LOG:
// Wednesday, January, 28 2009 -> function drawCaptchaInImage added to draw captcha on image

class math_captcha{

	var $arr_value1;
	var $arr_operator;
	var $arr_symbol;
	var $arr_value2;
	var $arr_result;
	var $img;
	var $img_x;
	var $img_y;
	var $img_background;
	var $img_color;
	var $img_string;
	
	function math_captcha(){
		$this->setRandom();
	}
	
	function setRandom(){
		$this->arr_value1 = rand(1,10);
		$this->arr_value2 = rand(1,10);
		$this->arr_operator = rand(1,2);
		
		if(($this->arr_operator==1)&&($this->arr_value1>$this->arr_value2)){
			$this->arr_result = $this->arr_value1 - $this->arr_value2;
			$this->arr_symbol = "-";
		} else {
			$this->arr_result = $this->arr_value1 + $this->arr_value2;
			$this->arr_symbol = "+";
		}
		
		$_SESSION['session_math_captcha_value1'] = $this->arr_value1;
		$_SESSION['session_math_captcha_value2'] = $this->arr_value2;
		$_SESSION['session_math_captcha_operator'] = $this->arr_symbol;
		$_SESSION['session_math_captcha_result'] = $this->arr_result;
	}
	
	function getValue1(){
		return $this->arr_value1;
	}
	
	function getValue2(){
		return $this->arr_value2;
	}
	
	function getOperator(){
		return $this->arr_symbol;
	}
	
	function getResult(){
		return $this->arr_result;
	}
	
	function drawCaptchaInImage(){
		$this->img_x = 61;
		$this->img_y = 20;
		if(strlen($this->getValue1())>1){
			$this->img_x += 9;
		}
		if(strlen($this->getValue2())>1){
			$this->img_x += 9;
		}
		$this->img = imagecreate($this->img_x,$this->img_y);
		$this->img_background = imagecolorallocate($this->img, 240, 240, 240);
		$this->img_color = imagecolorallocate($this->img, 0, 0, 0);
		$this->img_string = $this->getValue1() . ' ' . $this->getOperator() . ' ' . $this->getValue2() . ' = ';
		imagestring($this->img, 4, 2, 2, $this->img_string, $this->img_color);
		header("Content-type: image/png");
		imagepng($this->img);
		imagecolordeallocate($this->img_color);
		imagecolordeallocate($this->img_background);
		imagedestroy($this->img);
		
	}
	
}

?>